import 'dart:async';
import 'dart:io';
import 'package:acaleph/model/base_model.dart';
import 'package:acaleph/model/test_detail_model.dart';
import 'package:acaleph/mvp/repository/test_detail_repository.dart';
import 'package:acaleph/utils/http_util.dart';
import 'package:dio/dio.dart';

class TestDetailRepositoryImpl implements TestDetailRepository{
  @override
  Future<BaseModel<TestDetailModel>> loadTestDetail(String id) {
    return _getTestDetail(id);
  }

}

Future<BaseModel<TestDetailModel>> _getTestDetail(String id) async {
  int code;

  String errorMsg;

  TestDetailModel testDetailModel;

  BaseModel<TestDetailModel> model;

  try {
    Response response = await new HttpUtil().post("https://dsn.apizza.net/mock/d9ece8d859722233416fa78ba498d717/newsDetail",data:{"id":id});

    code = response.statusCode;

    if (response.statusCode == HttpStatus.OK) {
      testDetailModel = TestDetailModel.fromJson(response.data["data"]);
    } else {
      errorMsg = '服务器异常';
    }
  } catch (exception) {
    print(exception.toString());
    errorMsg = '您的网络似乎出了什么问题';
  } finally {
    model =
    new BaseModel(code: code, errorMsg: errorMsg, data: testDetailModel);
  }

  return model;
}